import { ref } from 'vue'
import { AddChildCommodityTypeForm } from './types'
import { typeSave } from '@/api/goodsSet'

export default () => {
  const addChildCommodityTypeLoading = ref(false)

  const addChildCommodityTypeForm = ref<AddChildCommodityTypeForm>({
    id: '',
    name: '',
    status: false,
    childData: [],
    parentId: '',
  })

  const resetAddChildCommodityTypeForm = () => {
    addChildCommodityTypeForm.value = {
      id: '',
      name: '',
      status: false,
      childData: [],
      parentId: '',
    }
  }

  const addChildCommodityType = async () => {
    addChildCommodityTypeLoading.value = true
    const res = await typeSave(addChildCommodityTypeForm.value)
    addChildCommodityTypeLoading.value = false
    return res.data as { message: string; code: number; data: object; success: boolean }
  }

  return {
    addChildCommodityTypeForm,
    addChildCommodityTypeLoading,
    addChildCommodityType,
    resetAddChildCommodityTypeForm,
  }
}
